package com.farm.device.controller;

import com.farm.device.pojo.Device;
import com.farm.device.pojo.DeviceInfo;
import com.farm.device.service.DeviceInfoService;
import com.farm.entity.Result;
import com.farm.entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@RestController
@RequestMapping("/deviceInfo")
public class DeviceInfoController {



    @Autowired
    private DeviceInfoService deviceInfoService;

    @GetMapping("/findByDid/{did}")
    public Result findByDid(@PathVariable("did") Integer did){
        List <DeviceInfo> byDid = deviceInfoService.findByDid(did);
        if (byDid==null||byDid.size()<=0){
            return new Result(false, StatusCode.ERROR,"查询失败");
        }
        return new Result(true,StatusCode.OK,"查询成功",byDid);
    }

    @PostMapping("/add")
    public Result add(@RequestBody DeviceInfo deviceInfo){
        try {
            deviceInfoService.add(deviceInfo);
                return new Result(true,StatusCode.OK,"添加成功");
            }
             catch (Exception e) {
            e.printStackTrace();
            return new Result(false,StatusCode.ERROR,"添加失败");
        }
    }

    @PutMapping("/update")
    public Result update(@RequestBody DeviceInfo deviceInfo ){
        try {
            deviceInfoService.update(deviceInfo);
            return new Result(true,StatusCode.OK,"修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,StatusCode.ERROR,"修改失败");

        }


    }

}
